package com.yimils.core.sys.beanUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yimils.core.sys.entity.UserEntity;
import com.yimils.core.sys.entity.UserRoleEntity;
import com.yimils.core.sys.entity.UserRppEntity;
import com.yimils.core.sys.service.UserRoleService;
import com.yimils.core.sys.service.UserRppService;
import com.yimils.core.sys.service.UserService;
import com.yimils.core.sys.vo.FeederVo;
import com.yimils.core.sys.vo.HotelVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Component
public class FeederVoBeanUtils {

    @Resource(name = "userRoleService")
    private UserRoleService userRoleService;

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "userRppService")
    private UserRppService userRppService;

    public IPage<FeederVo> deepClone(IPage<UserEntity> src) {
        IPage<FeederVo> pages = new Page<>();
        pages.setCurrent(src.getCurrent());
        pages.setSize(src.getSize());
        pages.setTotal(src.getTotal());
        pages.setPages(src.getPages());
        pages.setRecords(cloneRecords(src.getRecords()));
        return pages;
    }


    public List<FeederVo> cloneRecords(List<UserEntity> src) {
        List<FeederVo> recordsVo = new ArrayList<>();
        for (UserEntity userEntity : src) {
            FeederVo vo = new FeederVo();
            vo.setId(userEntity.getId());
            vo.setName(userEntity.getName());
            vo.setNick(userEntity.getNick());
            vo.setMobile(userEntity.getMobile());

            QueryWrapper<UserRppEntity> qw = new QueryWrapper<>();
            qw.lambda().eq(UserRppEntity::getUserId, userEntity.getId());
            vo.setRpp(userRppService.list(qw));
            vo.setSuperiorId(userEntity.getSuperiorId());

            UserEntity hotel = userService.getById(vo.getSuperiorId());
            QueryWrapper<UserRoleEntity> qwR = new QueryWrapper<>();
            qwR.lambda().eq(UserRoleEntity::getUserId, hotel.getId())
                    .eq(UserRoleEntity::getType, UserRoleService.UR_HOTEL);
            UserRoleEntity hotelRole = userRoleService.getOne(qwR);
            vo.setHotelName(hotelRole.getHotelName());
            vo.setDevNum(0);
            vo.setActive(userEntity.getActive());
            vo.setCreateTime(userEntity.getCreateTime());
            qwR = new QueryWrapper<>();
            qwR.lambda().eq(UserRoleEntity::getUserId, userEntity.getId());
            UserRoleEntity self = userRoleService.getOne(qwR);
            vo.setType(self.getType());
            recordsVo.add(vo);
        }
        return recordsVo;
    }

}
